package ch.subgate.spritestudio.widgets;

import ch.subgate.spritestudio.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.Button;

public class ColorChooserButton extends Button {
	private Bitmap mOverlay;
	private int mCurrentColor;
	private Rect mDestR;

	public ColorChooserButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		// Get the button overlay
		mOverlay = BitmapFactory.decodeResource(this.getResources(), R.drawable.currentcolor_button);
		
		// Create rect object
		mDestR = new Rect();
		
		// By default set current color to black
		mCurrentColor = 0xff000000;
	}
	
	public void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		mDestR.set(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight());
		
		// Draw current color as background
		canvas.drawColor(mCurrentColor);
		
		// Overlay it with the button layout
		canvas.drawBitmap(
				mOverlay,
				null,
				mDestR,
				null);
	}
	
	public void setCurrentColor(int color) {
		mCurrentColor = color;
		
		// Request to redraw button surface
		invalidate();
	}
	
	public int getCurrentColor() {
		return mCurrentColor;
	}
	
	public void dismiss() {
		mOverlay.recycle();
	}

}
